# encoding=utf-8
import sys
import time
import traceback

from pymongo.errors import BulkWriteError
import copy

sys.path.append('..')
sys.path.append('../..')
sys.path.append('../../..')

from conf import mongo_conf37
from common_parser_lib.mongo import MongDb
# from pyquery import PyQuery as py
from collections import deque
from collections import defaultdict
import json

reload(sys)
sys.setdefaultencoding('utf8')

import logging

# logging.basicConfig(filename='../logs', filemode='aw+', level=logging.DEBUG)

provinc_dict = {
    'beijing': u'北京',
    'tianjin': u'天津',
    'chongqing': u'重庆',
    'shanghai': u'上海',
    'hebei': u'河北',
    'shanxicu': u'山西',
    'liaoning': u'辽宁',
    'jilin': u'吉林',
    'heilongjiang': u'黑龙江',
    'jiangsu': u'江苏',
    'zhejiang': u'浙江',
    'anhui': u'安徽',
    'fujian': u'福建',
    'jiangxi': u'江西',
    'shandong': u'山东',
    'henan': u'河南',
    'hubei': u'湖北',
    'hunan': u'湖南',
    'guangdong': u'广东',
    'hainan': u'海南',
    'sichuan': u'四川',
    'guizhou': u'贵州',
    'yunan': u'云南',
    'shenzhen': u'深圳',
    'shanxi': u'陕西',
    'gansu': u'甘肃',
    'qinghai': u'青海',
    'taiwan': u'台湾',
    'neimenggu': u'内蒙古',
    'guangxi': u'广西',
    'xizang': u'西藏',
    'ningxia': u'宁夏',
    'xinjiang': u'新疆',
    'xianggang': u'香港',
    'aomen': u'澳门'
}


class change_update_all:
    def __init__(self):
        self.db37 = MongDb(mongo_conf37['host'], mongo_conf37['port'], 'final_data', mongo_conf37['username'],
                           mongo_conf37['password'])

        self.sourceTable = 'enterprise_data_gov_haizhi'
        self.targetTable = 'enterprise_data_gov_new_2'

    def update(self):
        result = self.db.select(self.sourceTable, {"credit_code": {"$ne": None}}).limit(1005)
        lst = []
        i = 0
        num = 0
        begin_time = time.time()
        for item in result:
            model = copy.deepcopy(item)
            num += 1
            # if num<=23000000:
            #     if num % 500 == 0:
            #         print num
            #     continue
            try:

                model['province'] = str(provinc_dict.get(item.get('province', '')))
                credit_code = item.get('credit_code', None)
                reg_code = item.get('registered_code', None)
                reg_code = credit_code if credit_code != None and reg_code != None and len(credit_code) > len(
                    reg_code) else reg_code
                model["registered_code"] = reg_code
                lst.append(model)
                if len(lst) >= 500:
                    self._insert_info_batch(self.targetTable, lst, is_order=False)
                    del lst[:]
                if num % 500 == 0:
                    print num, "time_cost:", time.time() - begin_time
            except Exception:
                print traceback.format_exc()
                # logging.info(traceback.format_exc())
                # logging.info(str(item))
            finally:
                if len(lst) >= 500:
                    self._insert_info_batch(self.targetTable, lst, is_order=False)
                    del lst[:]
        self._insert_info_batch(self.targetTable, lst, is_order=False)
        del lst[:]

    def isNullAndEmpty(self, str):
        if str != None and str != '':
            return False
        return True

    def _insert_info_batch(self, table, lst, is_order=True, insert=True):
        if lst != None and len(lst) == 0: return
        dbtemp = self.db.db[table]
        bulk = dbtemp.initialize_ordered_bulk_op() if is_order else dbtemp.initialize_unordered_bulk_op()
        for item in lst:
            # print item
            if insert:
                bulk.insert(item)
            else:
                _id = item.pop('_id')
                bulk.find({'_id': _id}).upsert().update({'$set': item})
        try:
            bulk.execute({'w': 0})
            print ('insert_logs:' + str(len(lst)))
            # logging.info('insert_logs:' + str(len(lst)))
        except BulkWriteError as bwe:
            pass
            # logging.info(bwe.details)

    def getKeyWordList(self, filename):
        lstdeq = deque()
        with open(filename, 'r') as keyWordData:
            for item in keyWordData:
                lstdeq.append(item.strip())
        return lstdeq


import os

s = os.sep
docpath = '..' + s + 'doc/'
if __name__ == '__main__':
    changeModel = change_update_all()
    changeModel.update()
